package org.dbwebx.flex.control.chevron
{

	import flash.events.MouseEvent;
	import mx.collections.ArrayCollection;
	import mx.controls.Image;
	import mx.controls.Menu;
	import mx.core.UIComponent;
	import mx.effects.Glow;
	import org.dbwebx.flex.common.PairEntry;
	import org.dbwebx.flex.common.constants.StyleConstants;
	import org.dbwebx.flex.common.constants.StyleNames;
	import org.dbwebx.flex.common.util.css.CSSUtils;
	import org.dbwebx.flex.constants.Icons;

	[Event(name="chevronActionClick",type="org.dbwebx.flex.common.event.ChevronEvent")]
	public class Chevron extends Image
	{

		public static const CHEVRON_HOVER:int=1;

		public static const CHEVRON_OPEN:int=0;
		private static const CHEVRON_HOVER_ST:String="chevronHover";

		private static const CHEVRON_OPEN_ST:String="chevronOpen";

		private static const CLASS_NAME:String="Chevron";

		private static var effect:Glow=new Glow();

		private static function staticConstructor():void
		{
			CSSUtils.setDefaultStyles(CLASS_NAME, new PairEntry(StyleNames.HORIZOTNAL_ALIGN, StyleConstants.ALIGN_CENTER), new PairEntry(CHEVRON_OPEN_ST, Icons.ICON_CHEVRON_OPEN), new PairEntry(CHEVRON_HOVER_ST, Icons.ICON_CHEVRON_HOVER));
		}

		public function Chevron(chevronClickHandler:IChevronHanlder=null)
		{
			super();
			_chevronClickHandler=chevronClickHandler;
			addEventListener(MouseEvent.CLICK, chevronClick, false, 0, true);
			addEventListener(MouseEvent.MOUSE_OVER, chevronOn, false, 0, true);
			addEventListener(MouseEvent.MOUSE_OUT, chevronOff, false, 0, true);
			buttonMode=true;
		}

		public var dataProvider:ArrayCollection;

		public var mainViewComponent:UIComponent;
		private var _chevronChanged:Boolean=true;


		/** @private chevron ClickHandler callback */
		private var _chevronClickHandler:IChevronHanlder;
		private var _chevronState:int;

		private var _dataChanged:Boolean;

		/** @private hide chevron */
		private var _hidden:Boolean;
		private var _hiddenNewValue:Boolean;

		private var _menu:Menu;

		private var _menuState:Boolean;

		/** @private chevron disappears when mouse out */
		private var _vanishing:Boolean;
		private var _vanishingNewValue:Boolean;

		private var _visibilityChanged:Boolean=true;

		public function chevronClick(event:MouseEvent):void
		{
			if (!_chevronClickHandler)
			{
				throw new Error("Chevron click handler was not defined.");
			}
			_chevronClickHandler.handleClick();
		}


		/**
		 * chevron ClickHandler callback
		 */
		public function get chevronClickHandler():IChevronHanlder
		{
			return _chevronClickHandler;
		}

		/**
		 * @private
		 */
		public function set chevronClickHandler(value:IChevronHanlder):void
		{
			_chevronClickHandler=value;
		}

		public function chevronOff(event:MouseEvent):void
		{
			_chevronState=CHEVRON_OPEN;
			_chevronChanged=true;
			invalidateProperties();
		}

		public function chevronOn(event:MouseEvent):void
		{
			_chevronState=CHEVRON_HOVER;
			_chevronChanged=true;
			invalidateProperties();
		}

		override public function set data(value:Object):void
		{
			super.data=value;
			_dataChanged=true;
			invalidateProperties();
		}


		/**
		 * hide chevron
		 */
		public function get hidden():Boolean
		{
			return _hidden;
		}

		/**
		 * @private
		 */
		public function set hidden(value:Boolean):void
		{
			_hiddenNewValue=value;
			_visibilityChanged=true;
			invalidateProperties();
		}

		public function get isMenuShown():Boolean
		{
			return (_menu != null && _menu.visible);
		}

		public function mouseOutHandler(event:MouseEvent):void
		{
			//visible = false;

		}

		public function mouseOverHandler(event:MouseEvent):void
		{
			//visible = true;
			effect.play([this]);
		}

		override public function styleChanged(styleProp:String):void
		{
			switch (styleProp)
			{
				case CHEVRON_OPEN_ST:
				case CHEVRON_HOVER_ST:
					_chevronChanged=true;
					invalidateProperties();
					break;
				default:
					super.styleChanged(styleProp);
			}
		}

		/**
		 * chevron disappears when mouse out
		 */
		public function get vanishing():Boolean
		{
			return _vanishing;
		}

		/**
		 * @private
		 */
		public function set vanishing(value:Boolean):void
		{

			_vanishingNewValue=value;
			_visibilityChanged=true;
			invalidateProperties();
		}

		override protected function commitProperties():void
		{
			if (_chevronChanged)
			{
				_chevronChanged=false;
				source=getStyle((_chevronState == CHEVRON_OPEN && !isMenuShown) ? CHEVRON_OPEN_ST : CHEVRON_HOVER_ST);
			}
			if (_visibilityChanged)
			{
				_visibilityChanged=false;
				if (_vanishing && !_hidden && parent)
				{
					parent.removeEventListener(MouseEvent.ROLL_OVER, mouseOverHandler);
					parent.removeEventListener(MouseEvent.ROLL_OUT, mouseOutHandler);
						//visible = true;
				}

				_vanishing=_vanishingNewValue;
				_hidden=_hiddenNewValue;

				includeInLayout=visible=!_hidden;

				if (_vanishing && !_hidden && parent)
				{
					parent.addEventListener(MouseEvent.ROLL_OVER, mouseOverHandler, false, 0, true);
					parent.addEventListener(MouseEvent.ROLL_OUT, mouseOutHandler, false, 0, true);
						//visible = false;
				}
			}
			if (_dataChanged)
			{
				_dataChanged=false;

					//TODO: hide opened menu
			}
			super.commitProperties();
		}

		staticConstructor();

	}
}

